package com.current.game.three.view;

import com.current.game.three.service.ThreeValueServiceImpl;
import com.current.game.two.TTFJpanel;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;

/**
 * @author baopan
 * @createTime 2024/5/12 22:31
 */
public class ThreeFrame {
    // 游戏板尺寸、方块大小等
    private static final int BOARD_SIZE = 4;
    private static final int TILE_SIZE = 100;

    private ThreeValueServiceImpl valueService = new ThreeValueServiceImpl();

    public void get2048Frame() {
        System.out.println("====>get2048Frame11111");
        JFrame frame = new JFrame("2048 Game");
        // 设置窗口大小不可变

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(BOARD_SIZE * TILE_SIZE + 17, BOARD_SIZE * TILE_SIZE + 40);

        int[][] ttfJValue = new int[4][4];
        valueService.doRandom(ttfJValue);
        ThreeJpanel ttfJpanel = new ThreeJpanel(ttfJValue);


        ttfJpanel.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int keyCode = e.getKeyCode();
                System.out.println("===>key:" + keyCode);

                // 你可以在这里添加额外的逻辑来处理特定的按键事件
                switch (keyCode) {
                    case 38://上
                    case 87://w
                        valueService.doUp(ttfJValue);
                        break;
                    case 40://下
                    case 83://s
                        valueService.doDown(ttfJValue);
                        break;
                    case 37://左
                    case 65://a
                        valueService.doLeft(ttfJValue);
                        break;
                    case 39://右
                    case 68://d
                        valueService.doRight(ttfJValue);
                        break;
                    default:
                        break;
                }
                ttfJpanel.repaint();
            }


        });

        frame.getContentPane().add(ttfJpanel); // 添加游戏面板到JFrame
        frame.setVisible(true);
        ttfJpanel.requestFocus(true);

    }


}
